package com.gin.kotlin.gf2gamecommunity.callback

import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.app.App.Companion.onFailed
import com.gin.kotlin.response.Res
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

abstract class AbstractNothingCallback: Callback<Res<Nothing>?> {
    abstract fun onSuccess(message:String)
    open fun finally(call: Call<Res<Nothing>?>){}

   final override fun onResponse(call: Call<Res<Nothing>?>, response: Response<Res<Nothing>?>) {
        when (val code = response.code()) {
            200 -> onSuccess(response.body()?.message ?: response.message())
            401 -> App.toast("需要先登录")
            else -> App.toast("请求异常: $code, ${response.message()}")
        }
        finally(call)
    }

    final override fun onFailure(call: Call<Res<Nothing>?>, throwable: Throwable) {
        onFailed(throwable)
        finally(call)
    }

    companion object {
        private val TAG = AbstractNothingCallback::class.java.simpleName
    }
}